package com.bwf.lgqingdan.mvp.model.impl;

import com.alibaba.fastjson.JSON;
import com.bwf.lgqingdan.entity.ResponseSearchArticle;
import com.bwf.lgqingdan.entity.ResponseSearchThing;
import com.bwf.lgqingdan.mvp.model.SearchDataListModel;
import com.bwf.lgqingdan.utils.http.lib.HttpUtils;
import com.bwf.lgqingdan.utils.http.lib.Request;
import com.bwf.lgqingdan.utils.http.qingdan.HttpClient;

/**
 * Created by Administrator on 2016/11/20.
 */

public class SearchDataListModelImpl implements SearchDataListModel{

    @Override
    public void loadArticleData(String url, final Callback callback) {
        Request.Builder builder = new Request.Builder()
                .url(url)
                .get();
        HttpClient.excute(builder, new HttpUtils.Callback() {
            @Override
            public void onResponse(String response) {
                ResponseSearchArticle responseSearchArticle = JSON.parseObject(response,ResponseSearchArticle.class);
                if(responseSearchArticle.getCode() == 0){
                    callback.loadArticleSuccess(responseSearchArticle.getData().getArticles());
                }else{
                    callback.loadFailed(responseSearchArticle.getMessage());
                }
            }

            @Override
            public void onError() {
                callback.loadFailed("连接服务器失败");
            }
        });
    }

    @Override
    public void loadThingData(String url, final Callback callback) {
        Request.Builder builder = new Request.Builder()
                .url(url)
                .get();
        HttpClient.excute(builder, new HttpUtils.Callback() {
            @Override
            public void onResponse(String response) {
                ResponseSearchThing responseSearchThing = JSON.parseObject(response,ResponseSearchThing.class);
                if(responseSearchThing.getCode() == 0){
                    callback.loadThingSuccess(responseSearchThing.getData().getThings());
                }else{
                    callback.loadFailed(responseSearchThing.getMessage());
                }
            }

            @Override
            public void onError() {
                callback.loadFailed("连接服务器失败");
            }
        });
    }
}
